* Core Analysis File

set more off

clear

cap n log close

log using 2a2-Prep-MSA.log, replace

******************************************************************
** CORE [1]: Self-employed, no limit on min observations        **
******************************************************************

use if metaread!=. using ./temp/2000-WorkerResults.dta, clear
gen cttotobs=1
gen ctselfobs=1 if classwkr==1
ren perwt cttot
gen ctself=cttot if classwkr==1
collapse (sum) ct*, by(ethn metaread) fast
sort ethn metaread
levelsof metaread, local(Mindex)
save ./temp/2000-MSA-Overage-Base, replace
keep if _n==1
for any eth metaread: replace X=.
drop ct*
gen OVER1=.
gen OVER2=.
save ./temp/2000-MSA-Overage-Reps, replace

foreach x of loc Mindex {
use if classwkr==1 & metaread==`x' using ./temp/2000-WorkerResults.dta, clear
sort ethn ind1990
egen indemp=total(perwt), by(ind1990)
egen emp=total(perwt)
egen indemp_gp=total(perwt), by(ethn ind1990)
egen emp_gp=total(perwt), by(ethn)
g overage=(indemp_gp/emp_gp)/(indemp/emp)
keep ethn metaread ind1990 indemp_gp overage
duplicates drop
egen temp1=total(indemp_gp), by(ethn)
g temp3=(indemp_gp/temp1)*overage
egen OVER1=total(temp3), by(ethn)
drop temp*
gsort ethn -indemp_gp
by ethn: g rank=_n
keep if rank<=3
egen temp1=total(indemp_gp), by(ethn)
g temp3=(indemp_gp/temp1)*overage
egen OVER2=total(temp3), by(ethn)
keep if rank==1
keep ethn metaread OVER*
append using ./temp/2000-MSA-Overage-Reps
sleep 1000
save ./temp/2000-MSA-Overage-Reps, replace
}

order ethn metaread OVER*
sort ethn metaread
merge ethn metaread using ./temp/2000-MSA-Overage-Base
drop _m
compress
save ./temp/2000-MSA-Overage-SelfEmp, replace
su

sleep 5000
erase ./temp/2000-MSA-Overage-Base.dta
erase ./temp/2000-MSA-Overage-Reps.dta

* End of program
log close